﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using RTSoft.Modules;

namespace RTSoft.SoureCode.Control.NhatKyXuat
{
    class nhatkyxuatctrl
    {
        DataProvider provider = new DataProvider();
        SqlCommand cmd = new SqlCommand();
        String strSQL = "";

        public void getData(ref String errConnect, ref String errSQL, ref DataTable tbl)
        {
            provider.Connection(ref errConnect);
            try
            {
                tbl = new DataTable();
                strSQL = "select * from NHATKYXUAT ";
                tbl = provider.LayDanhSach(strSQL);
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
            }
            finally
            {
                provider.Disconnect();
            }
        }

        public void Insert(ref String errConnect, ref String errSQL, nhatkyxuatinfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Insert into NHATKYXUAT";
                strSQL = strSQL + "(id_mathang,id_khachhang,ten_mathang,soluong,ngayxuat,id_nv,ten_nv)";
                strSQL = strSQL + " values";
                strSQL = strSQL + "(@id_mathang,@id_khachhang,@ten_mathang,@soluong,@ngayxuat,@id_nv,@ten_nv)";
                cmd.CommandText = strSQL;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@id_mathang", info.id_mathang);
                cmd.Parameters.AddWithValue("@id_khachhang", info.id_khachhang);
                cmd.Parameters.AddWithValue("@ten_mathang", info.ten_mathang);
                cmd.Parameters.AddWithValue("@soluong", info.soluong);
                cmd.Parameters.AddWithValue("@ngayxuat", info.ngayxuat);
                cmd.Parameters.AddWithValue("@id_nv", info.id_nv);
                cmd.Parameters.AddWithValue("@ten_nv", info.ten_nv);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void Update(ref String errConnect, ref String errSQL, nhatkyxuatinfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Update NHATKYXUAT SET  ";
                strSQL = strSQL + "id_mathang=@id_mathang,id_khachhang=@id_khachhang,ten_mathang=@ten_mathang,soluong=@soluong,ngayxuat=@ngayxuat,id_nv=@id_nv,ten_nv=@ten_nv Where  id_xuat = @id_xuat";
                cmd.CommandText = strSQL;

                cmd.Parameters.AddWithValue("@id_xuat", info.id_xuat);
                cmd.Parameters.AddWithValue("@id_mathang", info.id_mathang);
                cmd.Parameters.AddWithValue("@id_khachhang", info.id_khachhang);
                cmd.Parameters.AddWithValue("@ten_mathang", info.ten_mathang);
                cmd.Parameters.AddWithValue("@soluong", info.soluong);
                cmd.Parameters.AddWithValue("@ngayxuat", info.ngayxuat);
                cmd.Parameters.AddWithValue("@id_nv", info.id_nv);
                cmd.Parameters.AddWithValue("@ten_nv", info.ten_nv);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void Delete(ref String errConnect, ref String errSQL, int id)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                strSQL = "DELETE FROM NHATKYXUAT WHERE id_xuat=@RowID";
                cmd.Parameters.AddWithValue("@RowID", id);
                cmd.CommandText = strSQL;
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
                //' Thuc hien transaction 
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
        }
    }
}
